package com.hlt.controller.user;

import com.hlt.constant.JwtClaimsConstant;
import com.hlt.context.BaseContext;
import com.hlt.dto.LoginDTO;
import com.hlt.dto.PasswordUpdateDTO;
import com.hlt.dto.UserInfoUpdateDTO;
import com.hlt.entity.Users;
import com.hlt.properties.JwtProperties;
import com.hlt.result.Result;
import com.hlt.service.AdminService;
import com.hlt.service.IUsersService;
import com.hlt.utils.JwtUtil;
import com.hlt.vo.LoginVO;
import com.hlt.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lzf
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/users")
@Api(tags = "普通用户接口")
@Slf4j
public class UsersController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private IUsersService usersService;
    @Autowired
    private JwtProperties jwtProperties;

    /*
    用户登录
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录：{}", loginDTO);

        Users users = adminService.login(loginDTO);

        // 登录成功后，生成一个authorization，返回给前端
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put(JwtClaimsConstant.USER_ID,users.getUserId());
        String authorization = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                tokenMap);
        log.info("登录成功，生成的token为：{}", authorization);

        LoginVO userLoginVO = LoginVO.builder()
                .id(Long.valueOf(users.getUserId()))
                .userName(users.getUsername())
                .token(authorization)
                .build();

        return Result.success(userLoginVO);
    }

    /**
     * 管理员注册
     */
    @PostMapping("/register")
    @ApiOperation("注册")
    public Result<String> register(@RequestBody LoginDTO loginDTO) {
        log.info("注册：{}", loginDTO);
        adminService.register(loginDTO);
        return Result.success();
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/info")
    @ApiOperation("获取当前用户信息")
    public Result<UserInfoVO> getCurrentUserInfo() {
        // 从上下文获取用户ID（通过JWT拦截器设置）
        Long userId = BaseContext.getCurrentId();
        UserInfoVO userInfo = usersService.getUserInfoById(userId);
        return Result.success(userInfo);
    }

    /**
     * 修改个人信息
     */
    @PutMapping("/info")
    @ApiOperation("修改个人信息")
    public Result<String> updateUserInfo(@RequestBody UserInfoUpdateDTO dto) {
        Long userId = BaseContext.getCurrentId();
        usersService.updateUserInfo(userId, dto);
        return Result.success("个人信息修改成功");
    }

    /**
     * 修改密码
     */
    @PutMapping("/password")
    @ApiOperation("修改密码")
    public Result<String> updatePassword(@RequestBody PasswordUpdateDTO dto) {
        Long userId = BaseContext.getCurrentId();
        usersService.updatePassword(userId, dto.getOldPassword(), dto.getNewPassword());
        return Result.success("密码修改成功");
    }

}
